import org.jcsp.lang.*;

public class DisplayText implements CSProcess {

  private final int nPhilosophers;

  private final AltingChannelAccept philChannel;
  private final AltingChannelAccept forkChannel;
  private final AltingChannelInputInt securityChannel;


  private String space (int n) {
  	String s = "  ";
  	for (int i = 0; i < n; i++) {
  		s = s + "  ";
  	}
  	return s;
  }

  public DisplayText (int nPhilosophers,
                      PhilChannel philChannel,
                      ForkChannel forkChannel,
                      AltingChannelInputInt securityChannel

                      ) {
    this.nPhilosophers = nPhilosophers;
    this.philChannel = philChannel;
    this.forkChannel = forkChannel;
    this.securityChannel = securityChannel;

  }

  private interface inner extends CSProcess, PhilReport, ForkReport {};


  public void run () {

    new inner () {

      public void thinking (int id) {
        System.out.println (space(id) + "Philosopher " + id +
                            " is thinking ...");
      }

      public void hungry (int id) {
        System.out.println (space(id) + "Philosopher " + id +
                            " is hungry ...");
      }

      public void sitting (int id) {
        System.out.println (space(id) + "Philosopher " + id +
                            " is sitting ...");
      }

      public void eating (int id) {
        System.out.println (space(id) + "Philosopher " + id +
                            " is eating ...");
      }

      public void leaving (int id) {
        System.out.println (space(id) + "Philosopher " + id +
                            " is leaving ...");
      }

      public void forkUp (int id, int philId) {
        System.out.println (space(philId) + "Philosopher " + philId +
                            " has picked up fork " + id + " ...");
      }

      public void forkDown (int id, int philId) {
        System.out.println (space(philId) + "Philosopher " + philId +
                            " has put down fork " + id + " ...");
      }

      public void run () {

        final Alternative alt = new Alternative (
          new Guard[] {philChannel, forkChannel, securityChannel//, clockChannel
        		  }
        );
        final int PHIL = 0;
        final int FORK = 1;
        final int SECURITY = 2;


        System.out.println ("\n " + nPhilosophers + " philosophers starting \n");

        while (true) {
          switch (alt.fairSelect ()) {
            case PHIL:
              philChannel.accept (this);
            break;
            case FORK:
              forkChannel.accept (this);
            break;
            case SECURITY:
              final int nSitting = securityChannel.read ();
              System.out.println ("Security: " + nSitting + " sat down ...");
            break;

          }
        }

      }

    }.run ();

  }

}

